#!/usr/bin/env python3

import requests
from time import sleep

sleep(3)

api = 'https://api.coingecko.com/api/v3/coins'

currency = 'btc'
to_currency = 'usd'

try:
    r = requests.get(api).json()
except Exception:
    print(' Error')
    exit()

def get_currency(code, to_code, json):
    data = None

    for temp_data in json:
        if temp_data['symbol'] == code:
            data = temp_data

    if data is None:
        print('Not found!')
        return

    market_data = data['market_data']

    price = market_data['current_price'][to_code]
    change_24 = market_data['price_change_percentage_24h']
    change_7d = market_data['price_change_percentage_7d']

    res = [price, round(change_24, 2), change_7d]
    return res

price, h24, d7 = get_currency(currency, to_currency, r)
sign24 = '+' if h24 > 0 else ''

price = round(price)
change = f'{sign24}{h24}'

print(f'{change}%') # , 7d: {sign7}{d7}%')
